package com.winapi.project.controller;

import com.winapi.project.annotation.AuthCheck;
import com.winapi.project.common.BaseResponse;
import com.winapi.project.common.ResultUtils;
import com.winapi.project.model.vo.InterfaceInfoVO;
import com.winapi.project.model.vo.OrderVO;
import com.winapi.project.service.InterfaceInfoService;
import com.winapi.project.service.OrderService;
import com.winapi.project.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author yyzhang
 * @version 1.0
 */
@RestController
@RequestMapping("/analysis")
@Slf4j
public class AnalysisController {
    private static final String analysisLog="接口分析模块-";
    @Resource
    private InterfaceInfoService interfaceInfoService;
    @Resource
    private OrderService orderService;
    @Resource
    private ReportService reportService;
    @GetMapping("/top/interface/invoke")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<List<InterfaceInfoVO>> topNumber(){
        log.info(analysisLog+"调用次数前三的接口-开始调用接口....");
        List<InterfaceInfoVO> interfaceInfoVOList= interfaceInfoService.topNumber();
        log.info(analysisLog+"调用次数前三的接口-结束调用接口....");
            return ResultUtils.success(interfaceInfoVOList);
    }

    @GetMapping("/top/interface/invoke/excel")
    @AuthCheck(mustRole = "admin")
    public void topInvokeInterfaceInfoExcel(HttpServletResponse response) throws IOException  {
        log.info(analysisLog+"调用次数前三的接口数据导出excel-开始调用接口....");
        reportService.topInvokeInterfaceInfoExcel(response);
        log.info(analysisLog+"调用次数前三的接口数据导出excel-结束调用接口....");
    }

    @GetMapping("/top/interface/buy")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<List<OrderVO>> listTopBuyInterfaceInfo() {
        log.info(analysisLog+"购买次数前五的接口查询-开始调用接口....");
        List<OrderVO> list= orderService.listTopBuyInterfaceInfo();
        /*
         List<Order> list= orderService.listTopBuyInterfaceInfo();
         List<OrderVO> voList=list.stream().map(order->{
         OrderVO orderVO=new OrderVO();
         BeanUtils.copyProperties(order,orderVO);
         return orderVO;
         }).collect(Collectors.toList());
         */
        log.info(analysisLog+"购买次数前五的接口查询-结束调用接口....");
        return ResultUtils.success(list);
    }

    @GetMapping("/top/interface/buy/excel")
    @AuthCheck(mustRole = "admin")
    public void topBuyInterfaceInfoExcel(HttpServletResponse response) throws IOException {
        log.info(analysisLog+"购买次数前五的接口数据导出excel-开始调用接口....");
        reportService.topBuyInterfaceInfoExcel(response);
        log.info(analysisLog+"购买次数前五的接口数据导出excel-开始调用接口....");
    }

}
